package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.settings.service.impl.UserServiceImpl;
import com.bjpowernode.crm.utils.*;
import com.bjpowernode.crm.vo.PaginationVO;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.domain.Customer;
import com.bjpowernode.crm.workbench.domain.Tran;
import com.bjpowernode.crm.workbench.service.CustomerService;
import com.bjpowernode.crm.workbench.service.TranService;
import com.bjpowernode.crm.workbench.service.impl.CustomerServiceImpl;
import com.bjpowernode.crm.workbench.service.impl.TranServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: MyCRM
 * @description:
 * @author: 玉树临风的寒
 * @create: 2020-12-14 14:37:24
 **/
public class CustomerController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入客户控制器");

        String path = req.getServletPath();

        if ("/workbench/customer/getUserList.do".equals(path)) {

            getUserList(req, resp);

        } else if ("/workbench/customer/save.do".equals(path)) {

            save(req, resp);

        }else if ("/workbench/customer/pageList.do".equals(path)) {
            pageList(req, resp);
        }
        else if ("/workbench/customer/delete.do".equals(path)) {
            delete(req, resp);
        }
        else if ("/workbench/customer/detail.do".equals(path)) {
            detail(req, resp);
        }else if ("/workbench/customer/getUserListAndActivity.do".equals(path)) {
            getUserListAndActivity(req, resp);
        }else if ("/workbench/customer/update.do".equals(path)) {
            update(req, resp);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("执行客户修改操作");

        String  id= req.getParameter("id");
        String  owner= req.getParameter("owner");
        String  name= req.getParameter("name");
        String  website= req.getParameter("website");
        String  phone= req.getParameter("phone");
        String  contactSummary= req.getParameter("contactSummary");
        String  nextContactTime= req.getParameter("nextContactTime");
        String  description= req.getParameter("description");
        String  address= req.getParameter("address");
        String  editTime = DateTimeUtil.getSysTime();
        //修改人：当前登录用户
        String editBy = ((User)req.getSession().getAttribute("user")).getName();


        Customer c = new Customer();
        c.setOwner(owner);
        c.setNextContactTime(nextContactTime);
        c.setContactSummary(contactSummary);
        c.setEditBy(editBy);
        c.setName(name);
        c.setId(id);
        c.setEditTime(editTime);
        c.setAddress(address);
        c.setDescription(description);
        c.setWebsite(website);
        c.setPhone(phone);
        CustomerService cs = (CustomerService) ServiceFactory.getService(new CustomerServiceImpl());

        boolean flag = cs.update(c);

        PrintJson.printJsonFlag(resp,flag);

    }

    private void getUserListAndActivity(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("进入到查询客户信息列表和根据客户id查询单条记录的操作");

        String id = req.getParameter("id");

        CustomerService cs = (CustomerService) ServiceFactory.getService(new CustomerServiceImpl());

        Map<String,Object> map=cs.getUserListAndActivity(id);

        PrintJson.printJsonObj(resp,map);


    }

    private void detail(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        System.out.println("进入到跳转到详细信息页的操作");

        String id = req.getParameter("id");

        CustomerService cs = (CustomerService) ServiceFactory.getService(new CustomerServiceImpl());
        Customer c =cs.detail(id);

        req.setAttribute("c",c);

        req.getRequestDispatcher("/workbench/customer/detail.jsp").forward(req, resp);


    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {

        System.out.println("执行客户的删除操作");
        String ids[] =req.getParameterValues("id");
        CustomerService cs = (CustomerService) ServiceFactory.getService(new CustomerServiceImpl());

        boolean flag=cs.delete(ids);

        PrintJson.printJsonFlag(resp,flag);


    }

    private void pageList(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("进入到查询客户信息列表的操作 结合条件查询+分页查询");
        String name = req.getParameter("name");
        String owner = req.getParameter("owner");
        String phone = req.getParameter("phone");
        String website = req.getParameter("website");
        String pageNoStr = req.getParameter("pageNo");
        int pageNo =Integer.valueOf(pageNoStr);
        String pageSizeStr = req.getParameter("pageSize");
        int pageSize = Integer.valueOf(pageSizeStr);
        int skipCount = (pageNo-1)*pageSize;
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("name", name);
        map.put("owner", owner);
        map.put("phone",phone);
        map.put("website",website);
        map.put("skipCount",skipCount);
        map.put("pageSize",pageSize);

        CustomerService cs = (CustomerService) ServiceFactory.getService(new CustomerServiceImpl());

        PaginationVO<Customer> vo =cs.pageList(map);

        PrintJson.printJsonObj(resp,vo);

    }

    private void save(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("执行用户添加操作");

         String  id= UUIDUtil.getUUID();
         String  owner= req.getParameter("owner");
         String  name= req.getParameter("name");
         String  website= req.getParameter("website");
         String  phone= req.getParameter("phone");
         String  contactSummary= req.getParameter("contactSummary");
         String  nextContactTime= req.getParameter("nextContactTime");
         String  description= req.getParameter("description");
         String  address= req.getParameter("address");
         String createTime= DateTimeUtil.getSysTime();
         String createBy=((User)req.getSession().getAttribute("user")).getName();
        System.out.println("==================================="+owner);

        Customer c = new Customer();
        c.setOwner(owner);
        c.setNextContactTime(nextContactTime);
        c.setContactSummary(contactSummary);
        c.setCreateTime(createTime);
        c.setName(name);
        c.setId(id);
        c.setCreateBy(createBy);
        c.setAddress(address);
        c.setDescription(description);
        c.setWebsite(website);
        c.setPhone(phone);

        CustomerService cs = (CustomerService) ServiceFactory.getService(new CustomerServiceImpl());

        boolean flag = cs.save(c);

        PrintJson.printJsonFlag(resp,flag);

    }

    private void getUserList(HttpServletRequest req, HttpServletResponse resp) {

        System.out.println("取得用户信息列表");

        UserService us = (UserService) ServiceFactory.getService(new UserServiceImpl());

        List<User> uList = us.getUserList();
        PrintJson.printJsonObj(resp,uList);
    }
}



